package com.homegoods.shoppingcar.item;

import android.databinding.Bindable;
import android.text.SpannableStringBuilder;

import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.classify.model.Constant;
import com.homegoods.resource.util.Utils;
import com.homegoods.resource.widget.AddAndReduceWidget.OnCallBack;
import com.homegoods.shoppingcar.BR;
import com.homegoods.shoppingcar.R;
import com.homegoods.shoppingcar.model.GoodsBean;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.BaseItem;

/**
 * @author QiQi
 * @ClassName: GoodsItem
 * @date 2018/10/11
 * @description 购物车- 商品数据
 */
public class GoodsItem extends BaseItem<GoodsBean> {

    private OnCallBack onCallBack;

    public GoodsItem(GoodsBean data, MultiTypeAdapter adapter) {
        super(data);
        setOnClickListener(v -> {
            if (v.getId() == R.id.cb_goods) {
                toggle();
                v.setTag(data.getGroupPosition() + "," + data.getChildPosition());
                if (adapter.getOnItemClickListener() != null) {
                    adapter.getOnItemClickListener().onItemClicked(v, this);
                }
            } else if (v.getId() == R.id.cl_goods) {
                ARouter.getInstance()
                        .build(ARouterPath.PATH_GOODS_DETAIL)
                        .navigation();
            }
        });

        setOnCallBack((v, count) -> {
            getData().setNumber(String.valueOf(count));
            v.setTag(getData());
            if (adapter.getOnItemClickListener() != null) {
                adapter.getOnItemClickListener().onItemClicked(v, this);
            }
        });
    }

    private void toggle() {
        getData().setIsChildSelected(!getData().isChildSelected());
        notifyPropertyChanged(BR.selected);
    }

    public String getLogo() {
        return Constant.SERVER_URL + getData().getGoodsLogo();
    }

    public SpannableStringBuilder getPrice() {
        return Utils.getPrice(getData().getPrice());
    }

    public String getGoodName() {
        return getData().getGoodsName();
    }

    public String getGoodIntro() {
        return getData().getPdtDesc();
    }

    public OnCallBack getOnCallBack() {
        return onCallBack;
    }

    public void setOnCallBack(OnCallBack onCallBack) {
        this.onCallBack = onCallBack;
    }

    @Bindable
    public int getSelected() {
        return getData().isChildSelected() ? R.mipmap.icon_selected : R.mipmap.icon_unselected;
    }

    @Override
    public int getLayout() {
        return R.layout.shopping_item_goods;
    }
}
